<template>
<div class="vue-box">
  <div class="c-panel">
    <!-- 参数栏 -->
    <div class="c-title">检索参数</div>
    <el-form @submit.native.prevent>
      <sa-item v-model="p.search" type="text" name="模糊所属" />
      <sa-item v-model="p.level" type="enum" jtype="4" :jv="dims.ROLE_LEVEL" name="角色等级" :def="{}" :clearable="true" />
      <sa-item v-model="p.status" type="enum" jtype="4" :jv="dims.YES_OR_NO" name="启用状态" :def="{}" :clearable="true" />
      <div class="c-item">
        <el-button type="primary" icon="el-icon-search" @click="p.current = 1; f5()">查询</el-button>
      </div>
    </el-form>
    <!-- ------------- 快捷按钮 ------------- -->
    <sa-item type="fast-btn" show="add,get,export,reset" />
    <!-- 数据列表 -->
    <el-table ref="data-table" class="data-table" :data="dataList" border>
      <sa-td type="selection" width="45px" />
      <sa-td type="text" name="序号" prop="sort" width="80px" />
      <sa-td type="text" name="角色名称" prop="name" />
      <sa-td type="enum" name="角色等级" prop="level" :jv="dims.ROLE_LEVEL" />
      <sa-td type="enum" name="启用状态" prop="status" :jv="dims.YES_OR_NO" />
      <sa-td type="text" name="角色描述" prop="note" />
      <sa-td type="datetime" name="创建时间" prop="createTime" />
      <sa-td name="操作" fixed="right" width="220px">
        <template slot-scope="s">
          <el-button type="text" @click="update(s.row)">
            变更
          </el-button>
          <el-button type="text" @click="del(s.row)" v-if="s.row.isLock != 1">
            <span style="color: red;">
              删除
            </span>
          </el-button>
          <el-button type="text" @click="menu_setup(s.row)">
            角色权限
          </el-button>
        </template>
      </sa-td>
    </el-table>
    <!-- 分页 -->
    <div v-if="dataCount">
      <sa-item type="page" :curr.sync="p.current" :size.sync="p.size" :total="dataCount" @change="f5()" />
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: 'role-list',
  data() {
    return {
      p: { // 查询参数
        current: 1,
        size: 10,
        search: null,
        status: null,
        level: null,
        sortField: null
      },
      dataCount: null,
      // 数据集合
      dataList: [],
      // 缓存字典
      dims: {},
    }
  },
  created() {
    this.dims = sa.$sys.getCurd()
    this.f5();
    // 监听表单回车执行查询
    sa.onInputEnter();
  },
  methods: {
    // 刷新
    f5: function () {
      sa.ajax('/role/getList', this.p, function (res) {
        // 数据
        this.dataList = res.data;
        // 数据总数
        this.dataCount = res.dataCount;
        // 刷新表格高度
        sa.f5TableHeight();
      }.bind(this));
    },

    // 添加
    add: function () {
      sa.showModel('添加', () => import('./add'), {
        dims: this.dims
      });
    },

    // 查看详情
    getInfo: function (data) {
      sa.showModel('详情', () => import('./info'), {
        id: data.id,
        dims: this.dims
      });
    },

    // 查看 - 根据选中的
    getBySelect: function (data) {
      var selection = this.$refs['data-table'].selection;
      if (selection.length === 0) {
        return sa.msg('请至少选择一条数据')
      }
      this.getInfo(selection[0]);
    },

    // 变更
    update: function (data) {
      data.dims = this.dims
      sa.showModel('角色变更', () => import('./update'), data);
    },

    // 变更用户的状态
    updateStatus: function (data) {
      sa.ajax('/role/updateStatus', {
        id: data.id,
        status: data.status
      }, function (res) {
        sa.msg(res.msg);
      }, {
        type: 'get'
      });
    },
    // 删除
    del: function (data) {
      if (data.isLock == 1) {
        return sa.alert('此角色是维持系统正常运行的重要角色，已被锁定，不可删除');
      }
      sa.confirm('是否删除，此操作不可撤销', function () {
        sa.ajax('/role/delete', {
          id: data.id
        }, function (res) {
          sa.ok('删除成功');
          sa.f5TableHeight(); // 刷新表格高度
        }.bind(this), {
          type: 'get'
        })
      }.bind(this));

    },
    // 变更权限菜单
    menu_setup: function (data) {
      const title = '为 [' + data.name + '] 分配权限';
      sa.showModel(title, () => import('./setup'), {
        btn: false,
        roleId: data.id
      });
    }
  }
}
</script>

<style scoped>
.el-tag {
  border-radius: 0px;
}
</style>
